package com.csdj.t309.geamtest.controller;


import com.csdj.t309.geamtest.common.CacheConstant;
import com.csdj.t309.geamtest.common.WebResult;
import com.csdj.t309.geamtest.common.WebResultMsgCode;
import com.csdj.t309.geamtest.entity.Jifen;
import com.csdj.t309.geamtest.entity.User;
import com.csdj.t309.geamtest.service.*;
import com.csdj.t309.geamtest.util.CheckSign;
import com.csdj.t309.geamtest.util.Md5Utils;
import com.csdj.t309.geamtest.util.ShiJianUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 田港
 * @since 2022-01-01
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;
    @Resource
    private RedisService redisService;
    @Resource
    private IJifenService iJifenService;
    private Integer num2 = 6;
    private Integer numlogin = 6;


    @PostMapping("/login")
    public WebResult login(@RequestParam(value = "email", required = true) String email,
                           @RequestParam(value = "password", required = true) String password) {
        String dePass = Md5Utils.md5(password);
        if(!redisService.getString("password").equals(dePass) && numlogin!=0) {
            Integer num1 = new ShiJianUtil().shiJian();
            redisService.setString("login"+email, "登录", num1);
            numlogin--;
            return new WebResult(WebResultMsgCode.SUCCESS).putData("num",500).putData("num2",numlogin);
        }else if(numlogin==0&&redisService.getString("login"+email)==null){
            numlogin=5;
            return new WebResult(WebResultMsgCode.SUCCESS).putData("num",500).putData("num2",numlogin);
        }else {
            if(redisService.getString("login"+email)==null || numlogin!=0){
                User user = userService.login(email, password);
                if (user != null) {
                    numlogin = 6;
                    return new WebResult(WebResultMsgCode.SUCCESS, user).putData("user", user).putData("num",1);
                }
                return new WebResult(WebResultMsgCode.USER_PWD_ERROR).putData("num",0);
            }
            return new WebResult(WebResultMsgCode.SUCCESS).putData("num",0);
        }
    }

    @GetMapping("/selpassword")
    public WebResult selPassword(@RequestParam(value = "email", required = true) String email){
        User user = userService.selpassword(email);
        if(user!=null){
            redisService.setString("password",user.getPassword(),600);
            return new WebResult(WebResultMsgCode.SUCCESS, user).putData("user", user);
        }
        return new  WebResult(WebResultMsgCode.SUCCESS, user).putData("user", 0);
    }

    @GetMapping("/adduser")
    public WebResult addUser(@RequestParam(value = "email", required = true) String email,
                             @RequestParam(value = "password", required = true) String password,
                             @RequestParam(value = "inviteCode", required = false) String inviteCode,
                             @RequestParam(value = "yianzhen", required = false) String yianzhen,
                             HttpServletRequest request)
    {
        if(redisService.getString("yianzhen")==null){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("num",404);
        }else {
            if(!redisService.getString("yianzhen").equals(yianzhen) && num2!=0) {
                Integer num1 = new ShiJianUtil().shiJian();
                redisService.setString("add"+email, "注册", num1);
                num2--;
                return new WebResult(WebResultMsgCode.SUCCESS).putData("num",500).putData("num2",num2);
            }else if(num2==0&&redisService.getString("add"+email)==null){
                num2=5;
                return new WebResult(WebResultMsgCode.SUCCESS).putData("num",500).putData("num2",num2);
            }else {
                if(redisService.getString("add"+email)==null || num2!=0){
                    if(redisService.getString("yianzhen").equals(yianzhen)){
                        num2=6;
                        Integer num = userService.addUser(email, password, inviteCode,redisService.getObject(CacheConstant.HEADER_UID+request.getHeader(CacheConstant.HEADER_UID)));
                        return new WebResult(WebResultMsgCode.SUCCESS).putData("num", num);
                    }
                    return new WebResult(WebResultMsgCode.SUCCESS).putData("num", 100);
                }
                return new WebResult(WebResultMsgCode.SUCCESS).putData("num", 0);
            }
        }
    }

    @GetMapping("/emailTesting")
    public WebResult emailTesting(@RequestParam(value = "email", required = true) String email) {
        boolean ok = userService.emailTesting(email);
        return new WebResult(WebResultMsgCode.SUCCESS).putData("ok", ok);
    }

    @PostMapping("/seltupain")
    public WebResult seltupain(HttpServletRequest request) {
        User user = userService.seltupian(redisService.getObject(CacheConstant.HEADER_UID+request.getHeader(CacheConstant.HEADER_UID)));
        return new WebResult(WebResultMsgCode.SUCCESS).putData("user", user);
    }

    @PostMapping("/selqiandao")
    public WebResult selQianDao(HttpServletRequest request) {
        User user = userService.seltupian(redisService.getObject(CacheConstant.HEADER_UID+request.getHeader(CacheConstant.HEADER_UID)));
        return new WebResult(WebResultMsgCode.SUCCESS).putData("user", user);
    }

    @GetMapping("/updatename")
    public WebResult updatename(@RequestParam(value = "name", required = false) String name,
                                HttpServletRequest request) {
        Integer id = redisService.getObject(CacheConstant.HEADER_UID+request.getHeader(CacheConstant.HEADER_UID));
        User user = new User();
        user.setId(id);
        user.setName(name);
        Integer integer = userService.updatename(user);
        if (integer > 0) {
            return new WebResult(WebResultMsgCode.SUCCESS).putData("integer", integer);
        }
        return new WebResult(WebResultMsgCode.SUCCESS).putData("integer", 0);
    }

    @GetMapping("/updateqiandao")
    public WebResult updateqiandao(@RequestParam(value = "qiandao", required = false) Integer qiandao,
                                   @RequestParam(value = "integrate", required = false) Integer integrate,
                                   HttpServletRequest request) {
        String uid = request.getHeader(CacheConstant.HEADER_UID);
        Integer id = redisService.getObject(CacheConstant.HEADER_UID+uid);
        if(redisService.getString("aa"+id)!=null){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("integer", 0);
        }else {
            Integer num1 = new  ShiJianUtil().shiJian();
            redisService.setString("aa"+id,""+qiandao,num1);
            User user = new User();
            Jifen jifen = new Jifen();
            user.setId(id);
            user.setQiandao(qiandao);
            if(qiandao>=15){
                jifen.setJiNum("+" + 15);
                jifen.setJiRemainder(integrate+15);
                user.setIntegrate(integrate+15);
            }else {
                jifen.setJiNum("+" + qiandao);
                jifen.setJiRemainder(integrate+qiandao);
                user.setIntegrate(integrate+qiandao);
            }
            Integer integer = userService.updatename(user);
            jifen.setJiUserId(id);
            jifen.setJiOperateType("签到");
            Date date = new Date();
            jifen.setJiOperateDate(date);
            iJifenService.addJiFen(jifen);
            if (integer > 0) {
                return new WebResult(WebResultMsgCode.SUCCESS).putData("integer", integer);
            }
            return new WebResult(WebResultMsgCode.SUCCESS).putData("integer", 0);
        }
    }

    @PostMapping("/hash")
    public WebResult hash(@RequestParam(value = "name", required = false) String username,
                          @RequestParam(value = "name", required = false) String email,
                          @RequestParam(value = "name", required = false) String nickname,
                          @RequestParam(value = "name", required = false) String desc,
                          @RequestParam(value = "name", required = false) String id,
                          @RequestParam(value = "name", required = false) String sign) {
        TreeMap<String, String> map = new TreeMap<>();
        map.put("username", username);
        map.put("email", email);
        map.put("nickname", nickname);
        map.put("desc", desc);
        map.put("id", id);
        map.put("sign", sign);
        if (!CheckSign.checkSign(map, sign)) {
            return new WebResult(WebResultMsgCode.FAIL);
        }
        return new WebResult(WebResultMsgCode.SUCCESS);
    }


}
